package lc.q101_150;

import lc.TreeNode;

public class Q114 {

    public void flatten(TreeNode root) {
        // 左节点的最右节点是右节点的前驱
        // 假设左节点的最右节点是pre，那么就是pre.right = root.right
        // 一直循环下去
        TreeNode curr = root;
        while (curr != null) {
            if (curr.left != null) {
                // 寻找前驱
                TreeNode next = curr.left;
                TreeNode predessor = next;
                while (predessor.right != null) {
                    predessor = predessor.right;
                }
                predessor.right = curr.right;
                curr.left = null;
                curr.right = next;
            }
            curr = curr.right;
        }
    }
}
